import { addOrUpdateFreightAPI, deleteFreightAPI, fetchFreightListAPI } from '@/api/essentialDataAPI/freightAPI'

const actions = {
  /*
  * 获取运费模板列表的action
  *  */
  async fetchFreightListAction(context) {
    const { data } = await fetchFreightListAPI()
    context.commit('setFreightList', data)
  },
  /*
  * 删除运费模板的action
  *  */
  async deleteFreightAction(context, id) {
    await deleteFreightAPI(id)
    return '删除模板成功'
  },
  /*
  * 新增或者编辑模板的action
  *  */
  async addOrUpdateFreightAction(context, formData) {
    await addOrUpdateFreightAPI(formData)
    return formData.id ? '编辑模板成功' : '添加模板成功'
  }
}
const mutations = {
  /*
  * 设置运费模板列表的mutation
  *  */
  setFreightList(state, freightList) {
    state.freightList = freightList
  }
}
const state = () => {
  return {
    freightList: []
  }
}

export default {
  actions,
  mutations,
  state,
  namespaced: true
}
